﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace smartGardenForm
{
    public partial class dynamicAddControl
    {
        //实例化服务
        //smartGarden.SampleServiceSoapClient mysmartGardenService = new smartGarden.SampleServiceSoapClient();
        smartGardenNameSpace.SampleServiceSoapClient mysmartGardenService = new smartGardenNameSpace.SampleServiceSoapClient();

        smartGardenForm mysmartGardenForm = new smartGardenForm();

        private dataDeal mydataDeal = new dataDeal();

        #region 动态添加 手动模式  控件及事件  设备名标签-开关按钮-开启状态标签
        /// <summary>
        /// 动态添加 手动模式  控件  设备名标签-开关按钮-开启状态标签
        /// 按钮添加 点击事件
        /// </summary>
        /// <param name="modeId">模式Id</param>
        /// <param name="area">区域号</param>
        public void manModeAddControl(string modeId, string area, GroupBox dealGroupBox)
        {
            //清空groupBox控件内原有的控件
            dealGroupBox.Controls.Clear();

            string[] fac = mydataDeal.chkModeFac(modeId, area);
            int j = 0;
            string facilityName;
            string facility;
            //添加控件
            for (int i = 0; i < fac.Length; i++)
            {
                string[] fasSplit = fac[i].Split(new char[] { '+' });
                facilityName = fasSplit[0];
                facility = fasSplit[1];

                //设备名称标签
                Label lb = new Label();
                lb.Text = facilityName.ToString().Trim();
                lb.Size = new Size(50, 12);
                lb.Name = "label" + facility.ToString().Trim();
                lb.Location = new Point(44, 42 + i * 35);

                //设备开关，按钮
                Button bt = new Button();
                bt.Text = (mydataDeal.facNoToFacState(modeId, area, facility.ToString().Trim()) == "0") ? "开" : "关";
                bt.Name = "but" + facility.ToString().Trim();
                bt.Location = new Point(110, 37 + i * 35);
                bt.Click += new EventHandler(bt_Clik);

                //设备开启状态标签
                Label lb1 = new Label();
                lb1.Text = (mydataDeal.facNoToFacState(modeId, area, facility.ToString().Trim()) == "0") ? "关" : "开";
                lb1.Size = new Size(50, 12);
                lb1.Location = new Point(210, 42 + i * 35);

                //当前状态标签
                Label lb2 = new Label();
                lb2.Text = "当前状态";
                lb2.Size = new Size(55, 12);
                lb2.Location = new Point(199, 1);

                //添加控件
                dealGroupBox.Controls.Add(lb);
                dealGroupBox.Controls.Add(bt);
                dealGroupBox.Controls.Add(lb1);
                dealGroupBox.Controls.Add(lb2);
                j++;
            }
            dealGroupBox.Size = new Size(278, 36 + j * 35 + 20);
        }
        #endregion

        #region 设备按钮点击事件
        private void bt_Clik(object sender, EventArgs e)
        {
            //表示调用此函数的按钮
            Button butClik = (Button)(sender);

            string state = "";
            string facNo = butClik.Name.Substring(3, 2);

            //开启设备
            if (butClik.Text == "开")
            {
                state = "1";
                butClik.Text = "关";
            }

            //关闭设备
            else if (butClik.Text == "关")
            {
                state = "0";
                butClik.Text = "开";
            }

            string modeId = mysmartGardenService.GetModeId(mysmartGardenForm.comboBoxModeName.Text);
            string area = int.Parse(mysmartGardenForm.comboBoxArea.Text) < 10 ? "0" + mysmartGardenForm.comboBoxArea.Text : mysmartGardenForm.comboBoxArea.Text;
            string action = facNo + state.ToString();
            string nodeNo = "01";
            string instruction = state == "1" ? "开启" : "关闭";
            instruction += mydataDeal.facNoToFacName(facNo);

            //指令插入数据库
            string sqlcomm = "INSERT INTO actionInstruction(modeId,orderfrom,area,facilitiesNo,mode,orderSend,instruction,actionTime) VALUES('" + modeId + "','PC','" + area + "','" + nodeNo + "','" + "MAN" + "','" + action + "','" + instruction + "',GETDATE())";
            mydataDeal.insertDb(sqlcomm);
            //数据库更新设备状态
            mydataDeal.updateDb(state, modeId, area, facNo);
        }
        #endregion
    }
}
